package com.fanhao.dllo.justforyou.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.activity.NewsViewPageInforActivity;
import com.fanhao.dllo.justforyou.activity.NewsViewpageWebView;
import com.fanhao.dllo.justforyou.entity.NewsRecyclerResult;
import com.fanhao.dllo.justforyou.entity.NewsViewPageResult;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class NewsTopFragment extends Fragment {
    private NetworkImageView adIv;//广告图片
    private TextView titleTv;//广告文字
    private String title;// 标题
    private String imgSrc;// 图片地址
    private String url;//链接id

    private List<NewsViewPageResult.HeadlineAdEntity> headline_ad;

    private FrameLayout frameLayout;
    private Context context;
    private int position;

    private static NewsTopFragment newsTopFragment;



    public static NewsTopFragment getInstance(String title, String imgSrc,String url, int itemposition) {
        NewsTopFragment newsTopFragment = new NewsTopFragment();

        //参数传递
        Bundle args = new Bundle();
        args.putString("title",title);
        args.putString("imgSrc", imgSrc);
        args.putString("url", url);
        args.putInt("itemposition",itemposition);
        newsTopFragment.setArguments(args);

        return newsTopFragment;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();

        if (bundle != null) {
            title = bundle.getString("title");
            imgSrc = bundle.getString("imgSrc");
            url = bundle.getString("url");
            position = bundle.getInt("itemposition");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View v = inflater.inflate(R.layout.fragment_newstop, null);
        adIv = (NetworkImageView) v.findViewById(R.id.adIv);
        titleTv = (TextView)v.findViewById(R.id.titleTv);
        titleTv.setText(title);// 设置标题
        frameLayout=(FrameLayout)v.findViewById(R.id.frameLayout);


        gitBitmap(imgSrc, adIv);

        return v;

    }

    private void gitBitmap(String imgSrc, NetworkImageView adIv) {
        ImageLoader imageLoader = VolleySingleton.getInstance().getImageLoader();
        adIv.setDefaultImageResId(R.mipmap.ic_launcher);
        adIv.setErrorImageResId(R.mipmap.ic_launcher);
        adIv.setImageUrl(imgSrc,imageLoader);
    }



    @Override
    public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsViewPageResult.HeadlineAdEntity headlineAdEntity = headline_ad.get(position);
                if (headlineAdEntity.getTag() == "photoset") {


                String[] str=url.split("\\|");
                Log.i("lanou","url-->"+url+"---"+str[0]+"**"+str[1]);
               Intent intent =new Intent(getActivity(),NewsViewPageInforActivity.class);
                intent.putExtra("id", str[1]);
                getActivity().startActivity(intent);

                }

//                    Intent intent =new Intent(getActivity(),NewsViewpageWebView.class);
//
//                    getActivity().startActivity(intent);

            }
        });
    }
}
